<?php
/**
 * @purpose
 * @Author: MaDou
 * @Time  : 2022/7/27 18:34
 */

namespace App\Models\Trip\OrderBusiness\Repositories;

use App\Models\Plane\PlaneOrders\PlaneOrder;
use App\Models\Train\TrainOrders\TrainOrder;
use App\Models\Trip\OrderBusiness\BusinessOrderSnMap;
use App\Models\Trip\OrderBusiness\Repositories\Interfaces\BusinessOrderSnMapRepositoryInterface;
use App\Models\Trip\TripOrderRefund\TripOrderRefund;
use App\Trip\Repository\BaseRepository;
use Exception;
use Illuminate\Support\Collection;

class BusinessOrderSnMapRepository extends BaseRepository implements BusinessOrderSnMapRepositoryInterface
{
    public function __construct(BusinessOrderSnMap $model)
    {
        parent::__construct($model);
    }

    public function findByTradeNo(string $tradeNo): Collection
    {
        return $this->findBy([
            'trade_no' => $tradeNo,
        ]);
    }

    public function addOneMapByTrainOrder(TrainOrder $order, string $out_order_id): BusinessOrderSnMap
    {

        $business = BusinessOrderSnMap::BUSINESS_TYPE_BOOK;
        if ($order->isChangeNew()) {
            $business = BusinessOrderSnMap::BUSINESS_TYPE_CHANGE;
        } else if ($order->isSnapUp()) {
            $business = BusinessOrderSnMap::BUSINESS_TYPE_SNAP;
        }

        $data = [
            "trip_type"     => $order->getTripType(),
            "order_sn"      => $order->getOrderSn(),
            "trade_no"      => $order->getOutOrderSn(),
            "order_service" => $order->getOrderService(),
            "out_order_id"  => $out_order_id,
            "business"      => $business,
        ];

        return $this->addOneMap($data);
    }

    public function addOneMap(array $data): BusinessOrderSnMap
    {
        if ($map = $this->findOneBy($data)) {
            return $map;
        }
        return $this->create($data);
    }

    public function addOneMapByPlaneOrder(PlaneOrder $order, string $out_order_id): BusinessOrderSnMap
    {
        $business = BusinessOrderSnMap::BUSINESS_TYPE_BOOK;
        if ($order->isChangeNew()) {
            $business = BusinessOrderSnMap::BUSINESS_TYPE_CHANGE;
        }

        $data = [
            "trip_type"     => $order->getTripType(),
            "order_sn"      => $order->getOrderSn(),
            "trade_no"      => $order->getOutOrderSn(),
            "order_service" => $order->getOrderService(),
            "out_order_id"  => $out_order_id,
            "business"      => $business,
        ];

        return $this->addOneMap($data);
    }

    public function addOneMapByOrderTicketReturn(TripOrderRefund $refund, string $out_order_id): BusinessOrderSnMap
    {
        $business = BusinessOrderSnMap::BUSINESS_TYPE_TICKET_RETURN;
        $order    = $refund->getOrderInfo();
        !$order && throw new Exception("order not found");
        $data = [
            "trip_type"     => $order->getTripType(),
            "order_sn"      => $order->getOrderSn(),
            "trade_no"      => $refund->getRefundSn(),
            "order_service" => $order->getOrderService(),
            "out_order_id"  => $out_order_id,
            "business"      => $business,
        ];

        return $this->addOneMap($data);
    }
}